package controller;

import java.util.ArrayList;

import util.MoveType;
import entity.Board;
import entity.Movable;
import entity.Move;
import entity.Poem;
import entity.Row;
import entity.Word;
import gui.BoardApplication;

/**
 * This is the release poem controller.
 */
public class ReleaseController {
	private Board board;
	private BoardApplication gui;
	
	/**
	 * Constructor of release controller.
	 */
	public ReleaseController(BoardApplication gui, Board board){
		this.board = (board);
		this.gui = (gui);
	}
	
	/**
	 * Release a poem.
	 * The words in the poem would be removed from protectedWords and added to unprotectedWords. 
	 * Table in the board would be updated.
	 */
	public void release(Movable m, boolean checkpoint){
		ArrayList<Word> words = null;
		Move move = new Move(MoveType.UNPROTECT, checkpoint);
		move.addOldItem(m);
		
		if(m instanceof Word){
			words = new ArrayList<Word>();
			words.add((Word) m);
		}else if(m instanceof Row){
			words = ((Row) m).getWords();
		}else if(m instanceof Poem){
			words = ((Poem) m).getWords();
		}
		
		board.release(m);
		
		for(Word w : words){
			move.addNewItem(w);
		}
		
		board.addMove(move);
		
		gui.updateTable(board.getUnprotectedWords());
	}
	
	/**
	 * Return board.
	 */
	public Board getBoard() {
		return board;
	}

	/**
	 * Return gui.
	 */
	public BoardApplication getGui() {
		return gui;
	}
}
